package window;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 使用event time划分滚动窗口
 * non-key window和window function并行度都为1
 */
public class EventTimeTumblingWindowAllDemo1 {

    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());

        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        // flink 的时间精确到 毫秒级别
        // Watermarks (水位线，窗口触发机制)
        // 当前窗口的水位线 = 窗口中最大的eventTime - 延迟时间
        // 水位线 >= (窗口结束时间 - 1ms) 就会触发计算
        // BoundedOutOfOrdernessTimestampExtractor 有界的 允许数据乱序
        SingleOutputStreamOperator<String> watermarkDataStream = lines.assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<String>(Time.seconds(0)) {

            private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            @Override
            public long extractTimestamp(String s) {
                return Long.parseLong(s.split(",")[0]);
            }
        });

        // 处理数据
        SingleOutputStreamOperator<Integer> mapped = watermarkDataStream.map(new MapFunction<String, Integer>() {
            @Override
            public Integer map(String s) throws Exception {
                return Integer.parseInt(s.split(",")[1]);
            }
        });

        // 根据EventTime划分窗口
        AllWindowedStream<Integer, TimeWindow> window = mapped.windowAll(TumblingEventTimeWindows.of(Time.seconds(5)));

        // 聚合
        SingleOutputStreamOperator<Integer> summed = window.sum(0);

        // 打印
        summed.print();
        env.execute();
    }
}
